home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Tool Chest / Testing & Debugging / General tools / ZZPortList init / PortList.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-17  |  4.1 KB  |  210 lines  |  [TEXT/MPS ]

  1. /*
  2. **    PortList.c        PortList
  3. **
  4. **    This code is the property of Delta Tao Software, Inc.
  5. **    If you distribute this code or any derivative of it in any fashion
  6. **    we may and will come after your ass and take everything you have
  7. **    and everything you will every get anywhere in the universe for
  8. **    all of time.
  9. **
  10. **    **Warning** this code is god-like and perfect. When we work
  11. **    on it choirs of angels gather around and sing. Mortals must keep
  12. **    out. This code is so blindingly beautiful that your eyes will melt
  13. **    if you look at it.
  14. **
  15. **    Copyright (C) 1993 Delta Tao Software, Inc. All rights reserved.
  16. **    Use by license only. 408-730-9336.
  17. */
  18.  
  19. #include "Types.h"
  20. #include "Traps.h"
  21. #include "OSUtils.h"
  22. #include "SegLoad.h"
  23. #include "Files.h"
  24. #include "Resources.h"
  25. #include "SysEqu.h"
  26.  
  27.  
  28. #pragma parameter __A0 GetInitHdl(__D0)
  29. pascal Handle GetInitHdl( void ) = { 0x4E71 };
  30.  
  31. typedef pascal void (*GrafProcPtr)( void * );
  32.  
  33. typedef struct GlobalsRec GlobalsRec, *GlobalsPtr;
  34. struct GlobalsRec
  35.     {
  36.     GrafProcPtr        oldOpenPort;
  37.     GrafProcPtr        oldOpenCPort;
  38.     GrafProcPtr        oldInitPort;
  39.     GrafProcPtr        oldInitCPort;
  40.     GrafProcPtr        oldClosePort;
  41.     GrafProcPtr        oldCloseCPort;
  42.     };
  43.  
  44. typedef struct PortListRec PortListRec, *PortListPtr, **PortListHdl;
  45. struct PortListRec
  46.     {
  47.     short            plCount;
  48.     void *            plPort[1];
  49.     };
  50.  
  51. #define        gPortList    (* (PortListHdl *) PortList)
  52.  
  53. extern    GlobalsPtr        GetGlobals( void );
  54.  
  55. static    void *            PatchTrap( short, void * );
  56. static    pascal    void    PLOpenPort( void * );
  57. static    pascal    void    PLOpenCPort( void * );
  58. static    pascal    void    PLInitPort( void * );
  59. static    pascal    void    PLInitCPort( void * );
  60. static    pascal    void    PLClosePort( void * );
  61. static    pascal    void    PLCloseCPort( void * );
  62. static    void            CheckExists( void *, short );
  63.  
  64.  
  65. /*
  66. **    PLPortList()
  67. */
  68. void
  69. PLPortList()
  70. {
  71.     Handle            hInit;
  72.     GlobalsPtr        gl;
  73.     
  74.     /* get the handle to the code */
  75.     hInit = GetInitHdl();
  76.     
  77.     /* get pointer to globals */
  78.     gl = GetGlobals();
  79.     
  80.     /* patch the traps */
  81.     gl->oldOpenPort   = PatchTrap( _OpenPort,   PLOpenPort   );
  82.     gl->oldOpenCPort  = PatchTrap( _OpenCPort,  PLOpenCPort  );
  83.     gl->oldInitPort   = PatchTrap( _InitPort,   PLInitPort   );
  84.     gl->oldInitCPort  = PatchTrap( _InitCPort,  PLInitCPort  );
  85.     gl->oldClosePort  = PatchTrap( _ClosePort,  PLClosePort  );
  86.     gl->oldCloseCPort = PatchTrap( _CloseCPort, PLCloseCPort );
  87.     
  88.     /* we are installed */
  89.     /* make sure the code does not leave when */
  90.     /* the extension's resource fork is closed */
  91.     DetachResource( hInit );
  92. }
  93.  
  94.  
  95. /*
  96. **    PatchTrap()
  97. */
  98. static void *
  99. PatchTrap( short trap, void * newProc )
  100. {
  101.     void *        oldProc;
  102.     
  103.     oldProc = (void *) GetToolTrapAddress( trap );
  104.     SetToolTrapAddress( newProc, trap );
  105.     
  106.     return oldProc;
  107. }
  108.  
  109.  
  110. /*
  111. **    PLOpenPort()
  112. */
  113. static pascal void
  114. PLOpenPort( void * port )
  115. {
  116.     CheckExists( port, false );
  117.     (*GetGlobals()->oldOpenPort)( port );
  118. }
  119.  
  120.  
  121. /*
  122. **    PLOpenCPort()
  123. */
  124. static pascal void
  125. PLOpenCPort( void * port )
  126. {
  127.     CheckExists( port, false );
  128.     (*GetGlobals()->oldOpenCPort)( port );
  129. }
  130.  
  131.  
  132. /*
  133. **    PLInitPort()
  134. */
  135. static pascal void
  136. PLInitPort( void * port )
  137. {
  138.     CheckExists( port, false );
  139.     (*GetGlobals()->oldInitPort)( port );
  140. }
  141.  
  142.  
  143. /*
  144. **    PLInitCPort()
  145. */
  146. static pascal void
  147. PLInitCPort( void * port )
  148. {
  149.     CheckExists( port, false );
  150.     (*GetGlobals()->oldInitCPort)( port );
  151. }
  152.  
  153.  
  154. /*
  155. **    PLClosePort()
  156. */
  157. static pascal void
  158. PLClosePort( void * port )
  159. {
  160.     CheckExists( port, true );
  161.     (*GetGlobals()->oldClosePort)( port );
  162. }
  163.  
  164.  
  165. /*
  166. **    PLCloseCPort()
  167. */
  168. static pascal void
  169. PLCloseCPort( void * port )
  170. {
  171.     CheckExists( port, true );
  172.     (*GetGlobals()->oldCloseCPort)( port );
  173. }
  174.  
  175.  
  176. /*
  177. **    CheckExists()
  178. */
  179. static void
  180. CheckExists( void * port, short bFlag )
  181. {
  182.     short            count;
  183.     void **            pPort;
  184.     PortListPtr        pPortList;
  185.     
  186.     /* walk the port list */
  187.     pPortList = *gPortList;
  188.     pPort = &pPortList->plPort[0];
  189.     for ( count = pPortList->plCount - 1;  count >= 0;  --count )
  190.         {
  191.         /* if the port is is the list */
  192.         if ( *pPort++ == port )
  193.             {
  194.             /* notify if port should not be in list */
  195.             if ( bFlag == false )
  196.                 {
  197.                 DebugStr( "\pPort already in PortList." );
  198.                 }
  199.             return;
  200.             }
  201.         }
  202.     
  203.     /* port is not in the list */
  204.     /* notify if it should be */
  205.     if ( bFlag )
  206.         {
  207.         DebugStr( "\pPort not in PortList." );
  208.         }
  209. }
  210.